//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using System.ComponentModel;
using System.IO;

namespace CommunityServer.Controls 
{
    // *********************************************************************
    //  SiteStats
    //
    /// <summary>
    /// This server control display statistic information for the forums.
    /// </summary>
    // ***********************************************************************/
    public class SiteStats : TemplatedWebControl
    {
        #region Child Controls 

        System.Web.UI.WebControls.Image StatsImg;
        Label TotalUsers;
        Label TotalThreads;
        Label TotalPosts;
        Label NewThreadsInPast24Hours;
        Label NewPostsInPast24Hours;
        Label NewUsersInPast24Hours;
        HyperLink MostViewedThread;
        HyperLink MostActiveThread;
        HyperLink MostReadThread;
        Repeater TopUsersRepeater;
        HyperLink NewestUser;

        CSContext csContext = CSContext.Current;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if ((SkinTemplate == null) && (SkinName == null))
                ExternalSkinFileName = "Skin-Statistics.ascx";

            if (SkinName != null)
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            // If the user is already authenticated we have no work to do
            //
            if (!csContext.SiteSettings.EnableSiteStatistics)
            {
                this.Visible = false;
                return;
            }

            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();

            // Get the statistics
            //
            SiteStatistics siteStats = csContext.Statistics;
            
            // Find the stats image
            //
            if (null != StatsImg)
                StatsImg.ImageUrl = Globals.GetSkinPath() + "/images/icon_stats.gif";

            // Find the total Users
            //
            if (null != TotalUsers)
                TotalUsers.Text = siteStats.TotalUsers.ToString("n0");
            
            // Find the total threads
            //
            if (null != TotalThreads)
                TotalThreads.Text = siteStats.TotalThreads.ToString("n0");

            // Find the total posts
            //
            if (null != TotalPosts)
                TotalPosts.Text = siteStats.TotalPosts.ToString("n0");

            // New threads in past 24 hours
            //
            if (null != NewThreadsInPast24Hours)
                NewThreadsInPast24Hours.Text = siteStats.NewThreadsInPast24Hours.ToString("n0");

            // New posts in past 24 hours
            //
            if (null != NewPostsInPast24Hours)
                NewPostsInPast24Hours.Text = siteStats.NewPostsInPast24Hours.ToString("n0");

            // New users in past 24 hours
            //
            if (null != NewUsersInPast24Hours)
                NewUsersInPast24Hours.Text = siteStats.NewUsersInPast24Hours.ToString("n0");

            // Most viewed
            //
            if (null != MostViewedThread) 
            {
                MostViewedThread.Text = Formatter.CheckStringLength(siteStats.MostViewsSubject, 30);

                // Don't link if there are no posts yet
                if (siteStats.MostViewsPostID > 0)
                    MostViewedThread.NavigateUrl = Globals.GetSiteUrls().Post(siteStats.MostViewsPostID);
            }

            // Most posts
            //
            if (null != MostActiveThread) 
            {
                MostActiveThread.Text = Formatter.CheckStringLength(siteStats.MostActiveSubject, 30);

                // Don't link if there are no posts yet
                if (siteStats.MostActivePostID > 0)
                    MostActiveThread.NavigateUrl = Globals.GetSiteUrls().Post(siteStats.MostActivePostID);
            }

            // Most read
            //
            if (null != MostReadThread) 
            {
                MostReadThread.Text = Formatter.CheckStringLength(siteStats.MostReadPostSubject, 30);

                // Don't link if there are no posts yet
                if (siteStats.MostReadPostID > 0)
                    MostReadThread.NavigateUrl = Globals.GetSiteUrls().Post(siteStats.MostReadPostID);
            }

            // Most active user
            //
            if (null != TopUsersRepeater) 
            {
                ArrayList activeUsers = csContext.Statistics.ActiveUsers;
                
                try 
                {
                    if (activeUsers.Count > csContext.SiteSettings.MaxTopPostersToDisplay)
                        activeUsers.RemoveRange( csContext.SiteSettings.MaxTopPostersToDisplay, (activeUsers.Count - csContext.SiteSettings.MaxTopPostersToDisplay) );
                } 
                catch {}

                TopUsersRepeater.DataSource = activeUsers;
                TopUsersRepeater.DataBind();
            }

            // Newest user
            //
            if (null != NewestUser) 
            {
				User u = Users.FindUserByUsername(siteStats.NewestUser);
				if (u != null && u.UserID > 0)
					NewestUser.Text = u.DisplayName;
				else
					NewestUser.Text = siteStats.NewestUser;

                // Don't enable link if there are no posts yet
                if (siteStats.MostReadPostID > 0)
                    NewestUser.NavigateUrl = Globals.GetSiteUrls().UserProfile(siteStats.NewestUser);
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            StatsImg = (System.Web.UI.WebControls.Image) FindControl("StatsImg");
            TotalUsers = (Label) FindControl("TotalUsers");
            TotalThreads = (Label) FindControl("TotalThreads");
            TotalPosts = (Label) FindControl("TotalPosts");
            NewThreadsInPast24Hours = (Label) FindControl("NewThreadsInPast24Hours");
            NewPostsInPast24Hours = (Label) FindControl("NewPostsInPast24Hours");
            NewUsersInPast24Hours = (Label) FindControl("NewUsersInPast24Hours");
            MostViewedThread = (HyperLink) FindControl("MostViewedThread");
            MostActiveThread = (HyperLink) FindControl("MostActiveThread");
            MostReadThread = (HyperLink) FindControl("MostReadThread");
            TopUsersRepeater = (Repeater) FindControl("TopUsers");
            NewestUser = (HyperLink) FindControl("NewestUser");			
        }
		        
        #endregion
    }    
}
